package com.zls.controller;

import com.zls.pojo.Department;
import com.zls.service.DepartmentService;
import com.zls.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(path = "/admin/dept/")
public class DeptController {
    @Autowired
    private DepartmentService service;

    @RequestMapping(path = "show")
    public String show(Model model) {
        Department department = new Department();
        department.setId(1);
        department.setDeptName("研发部");
        department.setDeptDesc("负责公司产品研发");
        department.setCreatedate(new Date());
        model.addAttribute("dept", department);
        return "show";
    }

    @RequestMapping(path = "toadd")
    public String toAdd(Model model) {
        return "dept_toadd";
    }

    @RequestMapping(path = "doadd")
    public String doAdd(Model model, HttpServletResponse resp, Department dept) {
        boolean result = service.save(dept);
        resp.setContentType("text/html;charset=utf-8");
        if (result) {
            return "redirect:/admin/dept/List";
        } else if (!result) {
            try {
                resp.getWriter().println("<script>alert('名称重复,添加失败');history.go(-1);</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        } else {
            try {
                resp.getWriter().println("<script>alert('添加失败');history.go(-1);</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    @RequestMapping(path = "list")
    @ResponseBody
    public JsonResult toList() {
        List<Department> depts = service.list();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(depts);
        return jsonResult;
    }

    @RequestMapping(path = "toEdit")
    public String toEdit(Model model, String id) {
        Department deptById = service.getById(Integer.parseInt(id));
        model.addAttribute("dept", deptById);
        return "dept_toedit";
    }

    @RequestMapping(path = "doEdit")
    public String doEdit(Model model, HttpServletResponse resp, Department dept) {
        int result = service.editDept(dept);
        resp.setContentType("text/html;charset=utf-8");
        if (result > 0) {
            return "redirect:/admin/dept/List";
        } else if (result == -1) {
            try {
                resp.getWriter().println("<script>alert('名称重复,修改失败');history.go(-1);</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        } else {
            try {
                resp.getWriter().println("<script>alert('修改失败');history.go(-1);</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    @RequestMapping(path = "doDel/{id}")
    public String doDel(Model model, HttpServletResponse resp, @PathVariable(name = "id") String id) {
        int i = service.dropDeptById(Integer.parseInt(id));
        resp.setContentType("text/html;charset=utf-8");
        if (i > 0) {
            return "redirect:/admin/dept/List";
        } else if (i == -1) {
            try {
                resp.getWriter().println("<script>alert('该部门还有员工,删除失败');history.go(-1);</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        } else {
            try {
                resp.getWriter().println("<script>alert('删除失败');history.go(-1);</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
